package com.ysy.common.resp;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassDescription: 统一返回结果类，封装返回结果
 * @Author:姚斯羽
 * @Create:2025/3/12 14:00
 **/
@Slf4j
@Data
@Accessors(chain = true)
@Schema(name = "ResultData", description = "统一响应结果")
public class ResultData<T> {
    /** 结果状态 ,具体状态码参见枚举类ReturnCodeEnum.java*/
    @Schema(description = "状态码")
    private Integer code;
    @Schema(description = "状态信息")
    private String message;
    @Schema(description = "返回数据")
    private T data;
    @Schema(description = "时间戳")
    private long timestamp ;

    public ResultData (){
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);
        return resultData;
    }
    public static <T> ResultData<T> success() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        return resultData;
    }

    public static <T> ResultData<T> fail(Integer code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);

        return resultData;
    }

    public static <T> ResultData<T> fail(ReturnCodeEnum returnCodeEnum) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(returnCodeEnum.getCode());
        resultData.setMessage(returnCodeEnum.getMessage());

        return resultData;
    }

    public static <T> ResultData<T> fail(String msg) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(100);
        resultData.setMessage(msg);

        return resultData;
    }

    /**
     * 判断状态码是否为200
     * @return
     */
    public boolean successFlag() {
        return this.code.intValue()==200;
    }
}
